<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>卖家管理</title>
</head>
<body>
	<script type="text/javascript">
		var seller_url;
		$(function() {
			$('#seller_dg').datagrid({
				url : 'seller/getSellerList',
				rownumbers : true,
				pagination : true,
				showPageList : false,
				pageSize : 20,
				singleSelect : false,
				toolbar : [ {
					iconCls : 'icon-add',
					text : '添加',
					handler : function() {
						seller_add();
					}
				}, '-', {
					iconCls : 'icon-edit',
					text : '修改',
					handler : function() {
						seller_edit();
					}
				}, '-', {
					iconCls : 'icon-remove',
					text : '删除',
					handler : function() {
						seller_del();
					}
				} ],
				columns : [ [ {
					field : 'id',
					width : 10,
					checkbox : true
				}, {
					field : 'name',
					title : '用户名',
					width : 180
				}, {
					field : 'token',
					title : 'token',
					width : 100
				}, {
					field : 'create_time',
					title : '创建时间',
					width : 130
				},  {
					field : 'state',
					title : '状态',
					width : 100,
					formatter : function(index, row) {
						if (row.state == "0") {
							return "已禁用";
						} else if (row.state == "1") {
							return "正常 ";
						}
					}
				} ] ]
			});
		});

		/**
		 *添加
		 */
		function seller_add() {

			//取消表单验证
			$('#seller_fm').form('disableValidation');
			//显示编辑框的内容
			document.getElementById("seller_dlgc").style.display = "";
			$('#seller_fm').form('clear');
			$('#seller_dlg').dialog('open').dialog('setTitle', '新增卖家');
			//表单验证
			$('#seller_fm').form('enableValidation');
			$("#seller_state").combobox("setValue", "1");
			seller_url = 'seller/insert';
		}
		/**
		 *修改
		 */
		function seller_edit(index) {
			var array= $('#seller_dg').datagrid('getSelections');
			if(array.length==0){
				$.messager.alert('info',"请先选中一条记录！",'info');
				return;
			}else if(array.length>1){
				$.messager.alert('info',"每次只能修改一条记录！",'info');
				return;
			}
			
			//显示编辑框的内容
			document.getElementById("seller_dlgc").style.display = "";
			$('#seller_dg').datagrid('selectRow', index);// 关键在这里  
			var row = $('#seller_dg').datagrid('getSelected');
			if (row) {
				$('#seller_dlg').dialog('open').dialog('setTitle', '修改卖家信息');
				$('#seller_fm').form('clear');
				$('#seller_fm').form('load', {
					'seller.id' : row.id,
					'seller.name' : row.name,
					'seller.token' : row.token,
					'seller.create_time' : row.create_time,
					'seller.state' : row.state
				});
				seller_url = 'seller/update';
			}
		}
		
		/**
		*删除
		*/
		function seller_del() {
	    var array= $('#seller_dg').datagrid('getSelections');
	    var ids="";
	    for(var i=0; i<array.length; i++){//组成一个字符串，ID主键之间用逗号隔开
	        if(i!=array.length-1){
	          ids=ids+array[i].id+",";
	           }else{
	          	 ids=ids+array[i].id;
	           } 
	      } 
			
		if (array) {
				$.messager.confirm('删除确认', '您确定要删除吗?', function(r) {
					if (r) {
						$.post('seller/del', {
							ids : ids,
						}, function(result) {
							if (result.errorMsg) {
								$.messager.alert('Error', result.errorMsg,
										'error');
							} else {
								if (result.successMsg) {
									$.messager.alert('Success',
											result.successMsg, 'info');
								}
								$('#seller_dlg').dialog('close');
								$('#seller_dg').datagrid('reload');
							}
						}, 'json');
					}
				});
			}
		}

		/**
		 *保存
		 */
		function seller_save() {
			$('#seller_fm').form(
					'submit',
					{
						url : seller_url,
						data : $('#seller_fm').serialize(), // 从表单中获取数据
						type : "POST", // 设置请求类型为"POST"，默认为"GET"
						onSubmit : function() {
							return $(this).form('validate');
						},
						success : function(result) {
							var result = eval('(' + result + ')');
							if (result.errorMsg) {
								$.messager.alert('Error', result.errorMsg,
										'error');
							} else {
								if (result.successMsg) {
									$.messager.alert('Success',
											result.successMsg, 'info');
								}
								$('#seller_dlg').dialog('close'); // close the dialog
								$('#seller_dg').datagrid('reload');
								//location.href='user/initPage.xhtml'; // reload the user data
							}
						}
					});
		}
	</script>
	<!-- =================================================================添加、修改窗口==开始============================================================================ -->
	<div id="seller_dlgc" style="display: none;">
		<div id="seller_dlg" class="easyui-dialog"
			style="top: 20%; width: 500px; height: 240px;text-align:center"
			data-options="closed:true,buttons:'#seller_dlg-buttons'">
			<form id="seller_fm" method="post" >
				<table style="padding: 10px;margin: auto;" border=0 width=450px
					cellpadding="3" cellspacing="1">
					<tr>
						<td width="150px" align="center">用户名：</td>
						<td width="200px"><input type="hidden" id="userId"
							name="seller.id"> <input id="userName" name="seller.name"
							class="easyui-validatebox" style="width: 150px"
							data-options="required:true,validType:'length[1,40]'"></td>
					</tr>
					<tr>
						<td width="150px" align="center">TOKEN：</td>
						<td width="200px"><input id="token" name="seller.token"
							class="easyui-validatebox" style="width: 150px"
							data-options="required:true,validType:'length[1,40]'"></td>
					</tr>
					<tr>
						<td width="150px" align="center">是否启用：</td>
						<td width="200px"><input id="seller_state" name="seller.state"
							style="width: 150px" class="easyui-combobox"
							data-options="editable:false,valueField:'id',textField:'text',data:[{id:'0',text:'否'},{id:'1',text:'是'}]" />
						</td>
					</tr>
					<tr>
						<td width=150px align="center">创建时间：</td>
						<td width="200" id="create_time"><input
							name="seller.create_time" class="easyui-datetimebox"
							style="width: 150px"></td>
					</tr>
				</table>

			</form>
			<div id="seller_dlg-buttons">
				<a href="javascript:void(0)" class="easyui-linkbutton"
					onclick="seller_save()">保存</a> <a href="javascript:void(0)"
					class="easyui-linkbutton"
					onclick="javascript:$('#seller_dlg').dialog('close')">取消</a>
			</div>
		</div>
	</div>
	<!-- =================================================================添加、修改窗口==结束============================================================================ -->

	<table id="seller_dg"></table>
</body>
</html>